require("../../assets/fonts/iconfont.css");
require("../../assets/css/normalize.css");
require("../../assets/css/reset.css");
require("../../assets/css/swiper-bundle.min.css");
require("../../assets/css/tabbar.less");
require("./index.less");
require("../../assets/fontst/iconfont.css");
//模块引入
const dom = require("../../utils/dom.js");
console.log(dom);
const axios = require("axios");
//引入swiper
const Swiper = require("../../utils/swiper-bundle.min.js");

document.addEventListener("DOMContentLoaded", function () {
	//渲染tabbar
	dom.tabbar("index");
	//初始化
	var mySwiper = new Swiper(".swiper", {
		loop: true, //可选选项，自动滑动
		autoplay: {
			delay: 2000,
			stopOnLastSlide: false,
			disableOnInteraction: false,
		},
		//分页符
		pagination: {
			el: ".swiper-pagination",
		},
	}); //如果你初始化时没有定义Swiper实例，后面也可以通过Swiper的HTML元素来获取该实例

	//发请求获取用户个人信息
	axios.defaults.baseURL = "http://fitness.h5.itsource.cn:3701";
	let token = localStorage.getItem("token");
	axios.defaults.headers.common["Authorization"] = "Bearer " + token;
	axios.get("/api/user/info").then((res) => {
		// console.log(res);
		let data = res.data.data;
		//今日打卡排名
		dom.get("#rankNum").textContent = data.ranking;
		//徽章
		dom.get("#badgeNum").textContent = data.badges;
	});

	//打卡请求信息
	function renderClock() {
		axios.get("/api/user/clockInInfo").then((res) => {
			//渲染天数
			dom.get("#clockNum").textContent = res.data.data.clockCount;

			if (res.data.data.isClockIn) {
				dom.get("#clockBtn").disabled = true; //禁用按钮
				// dom.get("#clockBtn").style.background = "#999";
				dom.get("#clockBtn").style.border = "1px solid #999";
				dom.get("#clockBtn").style.color = "#999";
				dom.get("#clockBtn").textContent = "已打卡哟";
			}
		});
	}
	renderClock();

	//点击打卡事件
	dom.get("#clockBtn").addEventListener("click", function () {
		//发送请求
		axios.post("/api/user/clockIn").then((res) => {
			if (res.data.errno === 0) {
				alert("恭喜你，打卡成功！");
				renderClock();
			}
		});
	});
});
